<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>通过父文档查询子文档 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/has-parent.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/has-parent.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/has-parent.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/has-parent.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">通过父文档查询子文档</span>
</div>
<div class="navheader">
<span class="prev">
<a href="has-child.html">« 通过子文档查询父文档</a>
</span>
<span class="next">
<a href="children-agg.html">子文档聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="has-parent"></a>通过父文档查询子文档 (has_parent)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/55_Has_parent.asciidoc">edit</a>
</h2>
</div></div></div>
<p>虽然<code class="literal">nested</code>查询只能返回根文档，但是父文档和子文档本身是彼此独立并且可被单独查询的。我们使用<code class="literal">has_child</code>语句可以基于子文档来查询父文档，使用<code class="literal">has_parent</code>语句可以基于父文档来查询子文档。</p>
<p>和<code class="literal">has_child</code>非常相似，下面的<code class="literal">has_parent</code>查询将会返回所有在英国(UK)工作的员工：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/employee/_search
{
  "query": {
    "has_parent": {
      "type": "branch", <a id="CO281-1"></a><i class="conum" data-value="1"></i>
      "query": {
        "match": {
          "country": "UK"
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO281-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>返回父文档的<code class="literal">type</code>是<code class="word">branch</code>的所有子文档</p>
</td>
</tr>
</table>
</div>
<p><code class="literal">has_parent</code>查询也支持参数<code class="literal">score_mode</code>，但是该参数只支持两种值：<code class="literal">none</code>（默认）和<code class="literal">score</code>。每个子文档都只有一个父文档，因此这里不存在将多个评分规约为一个的情况，<code class="literal">score_mode</code>的取值只能是<code class="literal">score</code>或<code class="literal">none</code>。</p>
<div class="sidebar">
<div class="titlepage"><div><div>
<p class="title"><strong>不带评分的 has_parent 查询</strong></p>
</div></div></div>
<p>当<code class="literal">has_parent</code>查询用于非评分模式时（比如<code class="literal">filter</code>语句内），<code class="literal">score_mode</code>参数就不再起作用了。因为这种模式只是简单地包含或排除文档，没有评分，那么<code class="literal">score_mode</code>参数也就没有意义了。</p>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="has-child.html">« 通过子文档查询父文档</a>
</span>
<span class="next">
<a href="children-agg.html">子文档聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>